package com.tangji.signaltower.enums;

import com.fqgj.exception.common.ApplicationException;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: binary-option
 * @description
 * @author: frank
 * @create: 2020-06-17 16:04
 **/
public enum ProductEnum {

    EUR_USD("F_EUR_USD", 6, 0, 0, 1, "frxEURUSD"),
    GBP_USD("F_GBP_USD", 6, 0, 0, 1, "frxGBPUSD"),
    INR_USD("F_INR_USD", 8, 0, 0, 1, "frxINRUSD"),
    JPY_USD("F_JPY_USD", 8, 0, 0, 1, "frxJPYUSD"),
    ETH_USD("C_ETH_USD", 2, 0, 0, 1, "cryETHUSD"),
    BTC_USD("C_BTC_USD", 2, 0.005, -10, 10, "cryBTCUSD"),//2
    EUR_JPY("F_EUR_JPY", 4, 0, 0, 1, "frxEURJPY"),
    AUD_USD("F_AUD_USD", 5, 0, 0, 1, "frxAUDUSD"),
    AUD_EUR("F_AUD_EUR", 7, 0, 0, 1, "frxAUDEUR"),
//    XRP_USD("C_XRP_USD", 6, 0, 0, 1),
    ;


    ProductEnum(String productCode, int tailNum, double stepValue, int min, int max, String binProductCode) {
        this.productCode = productCode;
        this.tailNum = tailNum;
        this.stepValue = stepValue;
        this.min = min;
        this.max = max;
        this.binProductCode = binProductCode;
    }

    public static List<String> getAllBinProductCodes() {
        List<String> strings = new ArrayList<>();
        for (ProductEnum e : ProductEnum.values()) {
            strings.add(e.getBinProductCode());
        }
        return strings;
    }

    public static String getProductCodeByBinCode(String binProductCode) {

        for (ProductEnum e : ProductEnum.values()) {
            if (e.getBinProductCode().equals(binProductCode)) {
                return e.getProductCode();
            }
        }
        throw new ApplicationException("no find");
    }


    private String productCode;
    /**
     * 小数点位数
     */
    private int tailNum;
    /**
     * 随机添加的小数步长
     */
    private double stepValue;

    private int min;

    private int max;

    private String binProductCode;

    public String getBinProductCode() {
        return binProductCode;
    }

    public ProductEnum setBinProductCode(String binProductCode) {
        this.binProductCode = binProductCode;
        return this;
    }

    public String getProductCode() {
        return productCode;
    }

    public ProductEnum setProductCode(String productCode) {
        this.productCode = productCode;
        return this;
    }

    public ProductEnum setTailNum(int tailNum) {
        this.tailNum = tailNum;
        return this;
    }

    public int getTailNum() {
        return tailNum;
    }

    public double getStepValue() {
        return stepValue;
    }

    public ProductEnum setStepValue(double stepValue) {
        this.stepValue = stepValue;
        return this;
    }

    public int getMin() {
        return min;
    }

    public ProductEnum setMin(int min) {
        this.min = min;
        return this;
    }

    public int getMax() {
        return max;
    }

    public ProductEnum setMax(int max) {
        this.max = max;
        return this;
    }

    public static ProductEnum getEnumByProductCode(String productCode) {
        for (ProductEnum productEnum : ProductEnum.values()) {
            if (productEnum.getProductCode().equals(productCode)) {
                return productEnum;
            }
        }
        return null;
    }

    /**
     * 获取随机变动后的值
     *
     * @param productEnum
     * @param value
     * @return
     */
    public static BigDecimal getVariation(ProductEnum productEnum, BigDecimal value) {
        if (productEnum.getStepValue() == 0) {
            return value;
        }
        BigDecimal nValue = BigDecimal.valueOf(value.doubleValue() + getRandomValue(productEnum.getMin(), productEnum.getMax(), productEnum.getStepValue())).setScale(productEnum.getTailNum(), BigDecimal.ROUND_HALF_UP);
        return nValue;
    }

    /**
     * 获取随机变动后的值
     *
     * @param productEnum
     * @param value
     * @return
     */
    public static String getVariation(ProductEnum productEnum, String value) {
        if (productEnum.getStepValue() == 0) {
            return value;
        }
        return getVariation(productEnum, new BigDecimal(value)).stripTrailingZeros().toPlainString();
    }

    private static double getRandomValue(int min, int max, double stepValue) {
        int v = min + (int) (Math.random() * (max + 1 - min)); //生成从m到n的随机整数[m,n]
        return stepValue * v;
    }
}
